package parser;

import java.util.ArrayList;

import global.Constant;
import global.Methods;
import global.Time;

/**@author A0105661M*/
public class NewComponentAnalyser {

	public static int[] analyzeRecurrence(String[] recurrenceInfo,
			String[] feedback) {
		// to be implemented
		int[] info = new int[3];
		info[0] = recurrenceInfo.length;

		return info;
	}

	public static int[] analyzeTime(String[] timingInfo, int endingIndex,
			String[] feedback, boolean[] searchOption, int[] dateInfo) {
		return NewDateAndTimeAnalyzer.analyzeTimeInfo(timingInfo, endingIndex,
				feedback, searchOption, dateInfo);
	}

	public static int[] analyzeDate(String[] timingInfo, int endingIndex,
			String[] feedback, boolean[] searchOption) {
		return NewDateAndTimeAnalyzer.analyzeDateInfo(timingInfo, endingIndex,
				feedback, searchOption);
	}

	public static boolean isAll(String[] commandContent) {
		return IndexAnalyzer.isAll(commandContent);
	}

	public static ArrayList<Integer> analyzeIndex(String[] commandContent) {
		return IndexAnalyzer.analyzeIndex(commandContent);
	}

	public static int decideTaskType(String[] timingInfo, int endingIndex,
			String[] feedback, int[] timeInfo, int[] dateInfo) {
		return NewDateAndTimeAnalyzer.decideTaskType(timingInfo, endingIndex,
				feedback, timeInfo, dateInfo);
	}

	public static Time[] adjustDateAndTimeForSearch(int[] dateInfo,
			int[] timeInfo, String[] feedback, String keyword, String type,
			int undoneOption, boolean isAll) {
		return NewDateAndTimeAnalyzer.adjustDateAndTimeForSearch(dateInfo,
				timeInfo, feedback, keyword, type, undoneOption, isAll);
	}

	public static String extractTaskInfo(String[] taskAndTimingInfo, int end,
			String[] feedback, int[] timeInfo, int[] dateInfo) {
		String taskInfo;
		String taskInfoPiece1 = Constant.EMPTY_STRING;
		String taskInfoPiece2 = Constant.EMPTY_STRING;

		int[] positionInfo = new int[4];
		Methods.transerIntArray(timeInfo, 4, positionInfo, 0, 2);
		Methods.transerIntArray(dateInfo, 6, positionInfo, 2, 2);

		int smallestIndex = Methods.findMin(positionInfo);
		int largestIndex = Methods.findMax(positionInfo);

		if (smallestIndex - 1 >= 0) {
			String wordToCheck = taskAndTimingInfo[smallestIndex - 1];
			if (isTimingWord(wordToCheck)) {
				smallestIndex--;
			}
		} else if (smallestIndex != 0) {
			return Methods.stringArrayToString(taskAndTimingInfo, 0, end);
		}

		taskInfoPiece1 = Methods.stringArrayToString(taskAndTimingInfo, 0,
				smallestIndex);
		if (largestIndex > 0) {
			taskInfoPiece2 = Methods.stringArrayToString(taskAndTimingInfo,
					largestIndex + 1, end);
		}

		if (taskInfoPiece1.length() > 0 && taskInfoPiece2.length() > 0) {
			taskInfo = Constant.EMPTY_STRING + taskInfoPiece1 + Constant.SPACE
					+ taskInfoPiece2 + Constant.SPACE;
		} else {
			taskInfo = taskInfoPiece1 + taskInfoPiece2;
		}
		return taskInfo;
	}

	private static boolean isTimingWord(String wordToCheck) {
		String target = wordToCheck.toLowerCase();
		return (target.equals("from") || target.equals("at")
				|| target.equals("in") || target.equals("on")
				|| target.equals("during") || target.equals("until")
				|| target.equals("till") || target.equals("to")
				|| target.equals("by") || target.equals("before")
				|| target.equals("deadline") || target.equals("due"));
	}

}